package com.warrantyhotel.android.ui;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.db.QueriesCodes;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_Free_Table;
import com.warrantyhotel.android.model.Keys;
import com.warrantyhotel.android.util.BitmapUtils;
import com.warrantyhotel.android.util.BitmapUtils.OnFetchCompleteListener;
import com.warrantyhotel.android.util.JSONParser;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler.AsyncQueryListener;

public class InboxGetFreeDetail extends Activity implements AsyncQueryListener {

	private TextView txtInboxGetFreeDescription;
	private ImageView imgInboxBanner;
	private TextView txtInboxGetFreeTitle;
	private NotifyingAsyncQueryHandler mHandler;
	private String end_date = null;
	private String get_dree_id = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inbox_get_free_detail);

		txtInboxGetFreeTitle = (TextView) findViewById(R.id.inbox_free_credits_title);
		imgInboxBanner = (ImageView) findViewById(R.id.inbox_free_credit_banner);
		txtInboxGetFreeDescription = (TextView) findViewById(R.id.inbox_description_free_credits_description);
		String guid = getIntent().getExtras().getString(Keys._guid);
		mHandler = new NotifyingAsyncQueryHandler(getContentResolver(), this);
		retrieveNewsRSSbyId(guid);

	}

	private void retrieveNewsRSSbyId(String guid) {
		String[] projectionNews = { Inbox_Free_Table._ID, Inbox_Free_Table.ACTION_TEXT, Inbox_Free_Table.ACTION_TITLE, Inbox_Free_Table.GET_FREE_ID,
				Inbox_Free_Table.AWARD_CREDITS, Inbox_Free_Table.AWARD_UPLOADS, Inbox_Free_Table.BANNER_LINK, Inbox_Free_Table.END_DATE };
		mHandler.startQuery(QueriesCodes.INBOX_GET_FREE, Inbox_Free_Table.CONTENT_URI, projectionNews, Inbox_Free_Table.GET_FREE_ID + " = '" + guid + "'",
				null, null);
	}

	@Override
	public void onQueryComplete(int token, Object cookie, Cursor cursor) {
		switch (token) {
		case QueriesCodes.INBOX_GET_FREE:
			if (cursor != null) {
				startManagingCursor(cursor);
				if (cursor.moveToFirst()) {
					String description = cursor.getString(cursor.getColumnIndex(Inbox_Free_Table.ACTION_TEXT));
					txtInboxGetFreeDescription.setText(description);
					String banner_link = cursor.getString(cursor.getColumnIndex(Inbox_Free_Table.BANNER_LINK));
					get_dree_id = cursor.getString(cursor.getColumnIndex(Inbox_Free_Table.GET_FREE_ID));
					end_date = cursor.getString(cursor.getColumnIndex(Inbox_Free_Table.END_DATE));
					BitmapUtils.fetchImage(getApplicationContext(), banner_link, new OnFetchCompleteListener() {

						@Override
						public void onFetchComplete(Object cookie, Bitmap result) {
							imgInboxBanner.setImageBitmap(result);
						}
					});

					String title = cursor.getString(cursor.getColumnIndex(Inbox_Free_Table.ACTION_TITLE));
					txtInboxGetFreeTitle.setText(title);
				}
			}
			break;
		}

	}

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			if (end_date == null || end_date.equals("null")) {
				ContentValues values = new ContentValues();
				values.put(Inbox_Free_Table.ISVISIBLE, JSONParser.INBOX_ITEM_INVISIBLE);
				int count = getContentResolver().update(Inbox_Free_Table.CONTENT_URI, values, Inbox_Free_Table.GET_FREE_ID + "=?", new String[] { get_dree_id });
				Log.e("count", "" + count);
			}
		}
		// return false;//super.onKeyDown(keyCode, event);
		return super.onKeyDown(keyCode, event);
	}
	
	
}